if SERVER then
	AddCSLuaFile( "shared.lua" )
end


if CLIENT then
	SWEP.PrintName			= "Knife"	
	SWEP.Author				= "Counter-Strike"
	SWEP.Contact			= ""
	SWEP.Purpose			= ""
	SWEP.Instructions		= ""
	SWEP.DrawAmmo 			= false
	SWEP.DrawCrosshair 		= false
	SWEP.ViewModelFOV		= 70
	SWEP.ViewModelFlip		= false
	SWEP.CSMuzzleFlashes	= false
	SWEP.BounceWeaponIcon	= false
	
	SWEP.Slot				= 0
	SWEP.SlotPos			= 0
	SWEP.IconLetter			= "j"

	killicon.AddFont( "weapon_knife", "CSKillIcons", SWEP.IconLetter, Color( 255, 210, 0, 255 ) )

	surface.CreateFont( "csd", ScreenScale( 30 ), 500, true, true, "CSKillIcons" )
	surface.CreateFont( "csd", ScreenScale( 60 ), 500, true, true, "CSSelectIcons" )
end



SWEP.Category				= "Counter-Strike DS"

SWEP.Spawnable				= true
SWEP.AdminSpawnable			= true

SWEP.ViewModel 				= "models/weapons/v_knife_t.mdl"
SWEP.WorldModel 			= "models/weapons/w_knife_t.mdl" 

SWEP.Weight					= 5
SWEP.AutoSwitchTo			= false
SWEP.AutoSwitchFrom			= false
SWEP.HoldType				= "knife"
SWEP.NextIdle				= 0
SWEP.IdleDelay				= 15

SWEP.Primary.ClipSize		= -1
SWEP.Primary.Damage			= 35
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= true
SWEP.Primary.Ammo			= ""
SWEP.Primary.Delay			= 0.5

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Damage		= 65
SWEP.Secondary.Automatic	= true
SWEP.Secondary.Ammo			= "none"
SWEP.Secondary.Delay		= 1.1

SWEP.WallSound 				= Sound( "weapons/knife/knife_hitwall1.wav" )
SWEP.DeploySound			= Sound( "weapons/knife/knife_deploy1.wav" )
SWEP.Ragdollhit 			= Sound( "weapons/knife/knife_stab.wav" )


function SWEP:Think()
	if self.NextIdle < CurTime() and !self.Owner:KeyDown( IN_ATTACK ) and !self.Owner:KeyDown( IN_ATTACK2 ) then
		self:SendWeaponAnim( ACT_VM_IDLE )
		self.NextIdle = CurTime() + self.IdleDelay
	end
end

function SWEP:Initialize()  
 	self:SetWeaponHoldType( self.HoldType ) 
	util.PrecacheSound( "weapons/iceaxe/iceaxe_swing1.wav" )
	util.PrecacheSound( "weapons/knife/knife_hit1.wav" )
	util.PrecacheSound( "weapons/knife/knife_hit2.wav" )
	util.PrecacheSound( "weapons/knife/knife_hit3.wav" )
	util.PrecacheSound( "weapons/knife/knife_hit4.wav" )
	util.PrecacheSound( "weapons/knife/knife_slash1.wav" )
	util.PrecacheSound( "weapons/knife/knife_slash2.wav" )
 end 

function SWEP:Equip( NewOwner )
	self.Owner = NewOwner
end

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
	self.Weapon:SetNextPrimaryFire( CurTime() + 1 )
	self.Weapon:SetNextSecondaryFire( CurTime() + 1 )
	self.Weapon:EmitSound( self.DeploySound, 50, 100 )

	return true
end

function SWEP:PrimaryAttack()
	self.Owner:LagCompensation( true )

	local tr = {}
	tr.start = self.Owner:GetShootPos()
	tr.endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * 60
	tr.filter = self.Owner
	tr.mask = MASK_SHOT
	tr.mins = Vector( 0, -8, -2 )
	tr.maxs = Vector( 0, 8, 2 )
	local trace = util.TraceHull( tr )


	self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	self.Owner:SetAnimation( PLAYER_ATTACK1 )

	if trace.Hit then
		self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER )
		if SERVER then
			trace.Entity:TakeDamage( self.Primary.Damage, self.Owner, self )
		end
		if trace.Entity:IsPlayer() or string.find( trace.Entity:GetClass(), "npc" ) or string.find( trace.Entity:GetClass(), "prop_ragdoll" ) then
			self.Weapon:EmitSound( "weapons/knife/knife_hit"..math.random( 1, 4 )..".wav" )
		else
			self.Weapon:EmitSound( self.WallSound, 100, math.random( 90, 120 ) )		
			local trace = self.Owner:GetEyeTraceNoCursor()
			util.Decal( "ManhackCut", trace.HitPos + trace.HitNormal, trace.HitPos - trace.HitNormal )
		end
	else
		self.Weapon:EmitSound( Sound("weapons/knife/knife_slash"..math.random( 1, 2 )..".wav" ), 100, math.random( 90, 120 ) )
		self.Weapon:SendWeaponAnim( ACT_VM_MISSCENTER )
		self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
	end
	self.Owner:LagCompensation( false )
end

function SWEP:SecondaryAttack()
	self.Owner:LagCompensation( true )

	local tr = {}
	tr.start = self.Owner:GetShootPos()
	tr.endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * 60
	tr.filter = self.Owner
	tr.mask = MASK_SHOT
	tr.mins = Vector( 0, -8, -2 )
	tr.maxs = Vector( 0, 8, 2 )
	local trace = util.TraceHull( tr )

	self.Weapon:SetNextPrimaryFire( CurTime() + self.Secondary.Delay )
	self.Weapon:SetNextSecondaryFire( CurTime() + self.Secondary.Delay )
	self.Owner:SetAnimation( PLAYER_ATTACK1 )

	if trace.Hit then
		self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER )
		if SERVER then
			trace.Entity:TakeDamage( self.Secondary.Damage, self.Owner, self )
		end

		if trace.Entity:IsPlayer() or string.find( trace.Entity:GetClass(), "npc" ) or string.find( trace.Entity:GetClass(), "prop_ragdoll" ) then
 
			self.Weapon:EmitSound( self.Ragdollhit, 100, math.random( 90, 120 ) )
		else
			self.Weapon:EmitSound( self.WallSound, 100, math.random( 90, 120 ) )		
			local trace = self.Owner:GetEyeTraceNoCursor()
			util.Decal( "ManhackCut", trace.HitPos + trace.HitNormal, trace.HitPos - trace.HitNormal )
		end
	else
		self.Weapon:EmitSound( Sound("weapons/knife/knife_slash"..math.random( 1, 2 )..".wav" ), 100, math.random( 90, 120 ) )
		self.Weapon:SendWeaponAnim( ACT_VM_MISSCENTER )
		self.Weapon:SetNextPrimaryFire( CurTime() + self.Secondary.Delay )
	end
	self.Owner:LagCompensation( false )
end

function SWEP:Reload()
	return false
end

function SWEP:DrawWeaponSelection( x, y, wide, tall, alpha )	
	draw.SimpleText( self.IconLetter, "CSSelectIcons", x + wide/2, y + tall*0.2, Color( 255, 210, 0, 255 ), TEXT_ALIGN_CENTER )
end

function SWEP:DrawHUD()
	if self.Owner:ShouldDrawLocalPlayer() then return end

	local x, y = ScrW() / 2, ScrH() / 2
	
	surface.SetDrawColor( 255, 0, 0, 255 )
	surface.DrawLine( x - 30, y, x + 30, y )
end